window.$ = window.jQuery = require("jquery");
require("./rem");
require('../../css/reset2016.css', 'css|style');
require('../../css/public2016.css', 'css|style');
require('../../css/mall.css', 'css|style');

var API = require("../libs/ApiUtil");
var Cookies = require("js-cookie");
var pingpp = require('pingpp-js');

var GET = API.GetURLParams();
var orderId = GET["orderId"];
var platform = Cookies.get("isFromApp"); //0: weixin, 1: app
var isBaoXiang = Cookies.get("isBaoXiang");
var currPayType = 3;

if(platform == 0) {
    if(isBaoXiang == 1) { //宝箱
        $("#weChatLi").css("display", "none");
        $("#weChatSelected").removeClass("selected").addClass("unselected");
        $("#zhiFuBaoSelected").removeClass("unselected").addClass("selected");
        currPayType = 1;
    } else { //微信公众号
        $("#zhiFuBaoLi").css("display", "none");
        $("#zhiFuBaoSelected").removeClass("selected").addClass("unselected");
        $("#weChatSelected").removeClass("unselected").addClass("selected");
        currPayType = 3;
    }
} else { //路伯伯
    currPayType = 1;
}

function onChange(payType) {
    if(payType == 3) {
        $("#zhiFuBaoSelected").removeClass("selected").addClass("unselected");
        $("#weChatSelected").removeClass("unselected").addClass("selected");
        currPayType = 3;
    } else {
        $("#zhiFuBaoSelected").removeClass("unselected").addClass("selected");
        $("#weChatSelected").removeClass("selected").addClass("unselected");
        currPayType = 1;
    }
}

var header={
	   	"Content-Type": "application/json; charset=UTF-8",
        'X-Client-Agent':"weixin",
        'X-APIVersion':"2.0",
        'X-Client-ID':'123456',
        'X-Long-Token':Cookies.get("token"),
        };

$("#orderPay").click(function() {
    if(platform == 0) {
        var params = new Object();
        params.orderId = orderId;
        params.paymentChannel = currPayType;
        var data = JSON.stringify(params);

        API.postMallApi("order/getPaymentData", data, header, function (res) {
            if (res.errorCode == 0) {
                var charge = res.data.chargeObj;
                if(isBaoXiang == 1) {
                    getPaymentData(charge, "http://weixin.hzyisu.com/#/mall/paySuccess");
                } else {
                    pingpp.createPayment(charge, function (result, err) {
                        if (result == "success") {
                            success();
                        } else {
                            alert("支付失败, 请重试！");
                        }
                    })
                }
            }
        })
    } else {
        if(currPayType == 3) {
            currPayType = 2;
        }
        try {
            getPaymentData(orderId, currPayType, "http://weixin.hzyisu.com/#/mall/paySuccess");
        } catch(e){
        }

        try {
            window.lubobo.getPaymentData(orderId, currPayType, "http://weixin.hzyisu.com/#/mall/paySuccess");
        } catch (e) {
        }
    }
})


$("#zhiFuBaoLi").click(function () {
    onChange(1);
})

$("#weChatLi").click(function () {
    onChange(3);
})

function success() {
    window.location.href = "http://weixin.hzyisu.com/#/mall/paySuccess";
}
